<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Follow_user extends MY_Controller {

	//contructor class
	function __construct() {

		parent::__construct();
		$this->load->model('t_follow_user');
	}

	// register follow user
	public function register_follow_user(){
		$post = $this->validation_user();
		$follow = array();
		$follow[FL_USER_FROM_ID] = $post[FL_USER_FROM_ID];

		// validation user to
		if(!isset($post[FL_USER_TO_ID]) || empty($post[FL_USER_TO_ID])){
			parent::print_result('Missing key or value '. FL_USER_TO_ID);exit();
		}else{
			$follow[FL_USER_TO_ID] = $post[FL_USER_TO_ID];

		}
		

		// check is follow
		$isFollow = $this->is_followed($follow[FL_USER_FROM_ID], $follow[FL_USER_TO_ID]);
		if($isFollow == 2){
			parent::print_result(2, SUCCESS_VALUE);exit();
		}else if($isFollow == 1){
			$query = $this->t_follow_user->update_data_by_property(array(FL_USER_UNFOLLOW => 0, FL_USER_GET_NOTI => 1), $follow);
			parent::print_result(1, SUCCESS_VALUE);
		}else {
			$registed = $this->t_follow_user->set_data($follow, 1);
			if($registed == null){
				parent::print_result('Follow failed');exit();
			}else{
				parent::print_result($registed[0], SUCCESS_VALUE);
			}
		}
		// push notification to user to
		parent::push_notification_to_client_via_user_id($follow[FL_USER_TO_ID], $post[T_USERS][USER_NAME] . PUSH_MSG_FOLLOW_SUCCESS, PUSH_FOLLOW_ME , $follow[FL_USER_FROM_ID] );
		exit();
	}

	// unfollow user
	public function unfollow_user(){
		$post = $this->validation_user();
		$follow[FL_USER_FROM_ID] = $post[T_USERS][USER_ID];
		$follow[FL_USER_TO_ID] = $post[FL_USER_TO_ID];
		$query = $this->t_follow_user->update_data_by_property(array(FL_USER_UNFOLLOW => 1, FL_USER_GET_NOTI => 0), $follow);
		parent::print_result('Unfollow Success', SUCCESS_VALUE);
		exit();
	}

	/**
	 * check user has follow
	 * @param  integer $userFrom [description]
	 * @param  integer $userTo   [description]
	 * @return 0 : not follow yet | 1 has followed but unfollow | 2 had followed
	 */
	private function is_followed($userFrom = -1, $userTo = -1){
		if($userFrom == -1 || $userTo == -1){
			return 0;
		}

		$follow[FL_USER_FROM_ID] = $userFrom;
		$follow[FL_USER_TO_ID] = $userTo;

		$query = $this->t_follow_user->get_data_by_property($follow);
		if($query == null || count($query) == 0){
			return 0;
		}else if($query[0][FL_USER_UNFOLLOW] == 1){
			return 1;
		}else{
			return 2;
		}
	}

	public function is_following(){
		$post = parent::validation_user();

		$follow[FL_USER_FROM_ID] = $post[T_USERS][USER_ID];
		if(isset($post[FL_USER_TO_ID])){
			$follow[FL_USER_TO_ID] = $post[FL_USER_TO_ID];
		}else{
			parent::print_result('Missing key or value ' . FL_USER_TO_ID);exit();
		}

		$query = $this->t_follow_user->get_data_by_property($follow);

		if(empty($query)){
			parent::print_result('0', SUCCESS_VALUE);
		}else{
			if($query[0][FL_USER_UNFOLLOW] == 1){
				parent::print_result('0', SUCCESS_VALUE);
			}else{
				parent::print_result('1', SUCCESS_VALUE);
			}
		}

		exit();
	}

	/**
	 * register get notification
	 * @return [type] [description]
	 */
	public function register_get_notification(){
		$post = $this->validation_user();
		$follow = array();
		$follow[FL_USER_FROM_ID] 	= $post[FL_USER_FROM_ID];
		$follow[FL_USER_TO_ID] 		= $post[FL_USER_TO_ID];
		if(!isset($post[FL_USER_GET_NOTI])){
			parent::print_result('Missing value');exit();
		}

		$update = $this->t_follow_user->update_data_by_property(array(FL_USER_GET_NOTI => $post[FL_USER_GET_NOTI]),$follow);
		parent::print_result('Change settings success', SUCCESS_VALUE);
		exit();
	}

	/**
	 * validation user : user from, user to
	 * @return array || null
	 */
	public function validation_user(){
		$post = parent::validation_user();
		$post[FL_USER_FROM_ID] = $post[T_USERS][USER_ID];
		return $post;

	}

	public function get_user_following_total(){
		$post = $this->validation_user();
		$user_id = $post[T_USERS][USER_ID];
		$arrFollow = $this->t_follow_user->get_data_by_property(array(FL_USER_FROM_ID => $user_id, FL_USER_GET_NOTI => 1));
		if(empty($arrFollow)){
			parent::print_result(0, SUCCESS_VALUE);
		}else{
			parent::print_result(count($arrFollow), SUCCESS_VALUE);
		}
		exit();
	}

	/**
	 * user from
	 * @return [type] [description]
	 */
	public function get_user_following(){
		$post = $this->validation_user();

		if(isset($post[FL_USER_GET_NOTI])){
			$receive = $post[FL_USER_GET_NOTI];
		}else{
			$receive = 1;
		}

		if(isset($post[PAGE])){
			$page = $post[PAGE];
		}else{
			$page = -1;
		}

		if(isset($post[PER_PAGE])){
			$per_page = $post[PER_PAGE];
		}else{
			$per_page = -1;
		}

		$user_id = $post[T_USERS][USER_ID];
		$arrFollow = $this->t_follow_user->get_user_following($user_id, $receive, $page, $per_page);
		if($arrFollow == null || count($arrFollow) == 0){
			parent::print_result('No followed');
		}else{
			foreach ($arrFollow as $key => $value) {
				unset($arrFollow[$key][USER_PASSWORD]);
				unset($arrFollow[$key][USER_RESET_PASSWORD]);	
				unset($arrFollow[$key][USER_SECRET_QUESTION]);
				unset($arrFollow[$key][USER_SECRET_ANSWER]);
			}
			parent::print_result($arrFollow, SUCCESS_VALUE);
		}
		exit();
	}



	public function get_user_followed_total(){
		$post = $this->validation_user();
		if(!isset($post[FL_USER_TO_ID]) || empty($post[FL_USER_TO_ID])){
			$user_id = $post[T_USERS][USER_ID];
		}else{
			$user_id = $post[FL_USER_TO_ID];	
		}
		$arrFollow = $this->t_follow_user->get_data_by_property(array(FL_USER_TO_ID => $user_id, FL_USER_GET_NOTI =>1));
		if(empty($arrFollow)){
			parent::print_result(0, SUCCESS_VALUE);
		}else{
			parent::print_result(count($arrFollow), SUCCESS_VALUE);
		}
		exit();
	}

	/**
	 * user to
	 * @return [type] [description]
	 */
	public function get_user_followed(){
		$post = $this->validation_user();

		if(isset($post[FL_USER_GET_NOTI])){
			$receive = $post[FL_USER_GET_NOTI];
		}else{
			$receive = 1;
		}
		if(!isset($post[FL_USER_TO_ID]) || empty($post[FL_USER_TO_ID])){
			$user_id = $post[T_USERS][USER_ID];
		}else{
			$user_id = $post[FL_USER_TO_ID];	
		}
		
		$arrFollow = $this->t_follow_user->get_user_followed($user_id, $receive);
		if($arrFollow == null || count($arrFollow) == 0){
			parent::print_result('No followed');
		}else{
			parent::print_result($arrFollow, SUCCESS_VALUE);
		}
		exit();
	}

	public function get_user_followed_by_id_total(){
		$post = $this->validation_user();

		if(!isset($post[FL_USER_TO_ID]) || empty($post[FL_USER_TO_ID])){
			parent::print_result('Missing key or value ' . FL_USER_TO_ID);exit();
		}else{
			$userTo = $post[FL_USER_TO_ID];	
		}

		$query = $this->t_follow_user->get_data_by_property(array(FL_USER_TO_ID => $userTo,FL_PRO_UNFOLLOW => 0 ));
		if(empty($query)){
			parent::print_result(0, SUCCESS_VALUE);
		}else{
			parent::print_result(count($query), SUCCESS_VALUE);
		}
		exit();
	}

	public function get_user_followed_by_id(){
		$post = $this->validation_user();

		if(!isset($post[FL_USER_TO_ID]) || empty($post[FL_USER_TO_ID])){
			parent::print_result('Missing key or value ' . FL_USER_TO_ID);exit();
		}else{
			$userTo = $post[FL_USER_TO_ID];	
		}

		$userFrom = $post[T_USERS][USER_ID];
		
		$arrFollow = $this->t_follow_user->get_user_followed_by_id($userTo, $userFrom);
		if($arrFollow == null || count($arrFollow) == 0){
			parent::print_result('No followed');
		}else{
			foreach ($arrFollow as $key => $value) {
				unset($arrFollow[$key][USER_PASSWORD]);
				unset($arrFollow[$key][USER_RESET_PASSWORD]);
				unset($arrFollow[$key][USER_SECRET_QUESTION]);
				unset($arrFollow[$key][USER_SECRET_ANSWER]);
			}

			parent::print_result($arrFollow, SUCCESS_VALUE);
		}
		exit();
	}

	public function get_user_followed_($user_id = -1){
		$arrFollow = $this->t_follow_user->get_data_by_property(array(FL_USER_TO_ID => $user_id, FL_USER_GET_NOTI => 1));
		return $arrFollow;
	}
}